package cn.xwlin.configcenter.helper;

import cn.xwlin.configcenter.holder.ClientConfigCacheManager;
import cn.xwlin.configcenter.refresh.IWlinConfigChangeCallBack;

import java.util.List;


/**
 * @author xiang.liao
 * @create 2024/5/22
 */
public class CfgHelper {
  private ClientConfigCacheManager configManager;

  public CfgHelper() {
    configManager = new ClientConfigCacheManager();
    configManager.runConfigManager();
  }

  public <T> T getConfig(Class<T> clazz) {
    T value = null;
    if (this.configManager == null) {
      synchronized (CfgHelper.class) {
        if (this.configManager == null) {
          this.configManager = new ClientConfigCacheManager();
        }
      }
    }
    value = configManager.GetConfigValue(clazz.getSimpleName(), clazz);
    return value;
  }

  public <T> T getConfig(String configName, Class<T> clazz) {
    T value = null;
    if (this.configManager == null) {
      synchronized (CfgHelper.class) {
        if (this.configManager == null) {
          this.configManager = new ClientConfigCacheManager();
        }
      }
    }
    value = configManager.GetConfigValue(configName, clazz);
    return value;
  }

  public String getConfig(String configName) {
    if (this.configManager == null) {
      synchronized (CfgHelper.class) {
        if (this.configManager == null) {
          this.configManager = new ClientConfigCacheManager();
        }
      }
    }
    return configManager.GetConfigValueString(configName);
  }

  public void registerCustomCallback(String key, IWlinConfigChangeCallBack changeCallBackList) {
    if (null != configManager) {
      configManager.setCustomConfigChangeCallBack(key, changeCallBackList);
    }
  }
}
